package org.example.user.service.Impl;

import lombok.extern.slf4j.Slf4j;
import org.example.code.service.CodeFeignService;
import org.example.user.domain.SysToken;
import org.example.user.domain.SysUser;
import org.example.user.mapper.UserMapper;
import org.example.user.service.ITokenService;
import org.example.user.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

/**
 * @description: UserServiceImpl
 * @author: e
 * @createDate: 2021/5/30
 */
@Slf4j
@Service
public class UserServiceImpl implements IUserService {

    @Autowired(required = false)
    private UserMapper userMapper;

    @Autowired
    private ITokenService tokenService;


    @Autowired
    private CodeFeignService codeFeignService;


    @Override
    public Integer findEmailIsRegister(String email) {
        return userMapper.findEmailIsRegister(email);
    }

    /**
     * 注册 0成功，1注册失败，2注册码过期，3注册码错误.
     */
    @Override
    public int register(String email, String password, String code) {
        //判断当前邮箱是否被注册过
        if (this.findEmailIsRegister(email) == 1) {
            log.error("邮箱已被注册 {} 错误!", email);
            return 1;
        }
        if (email == null || password == null || code == null) {
            return 1;
        }

        //调用验证码服务，校验验证码是否正确
        int validateCode = codeFeignService.validate(email, code);
        if (validateCode == 1) {
            log.error("校验码 {} 错误!", code);
            return 3;
        } else if (validateCode == 2) {
            log.error("校验码 {} 超时!", code);
            return 2;
        } else if (validateCode == -1) {
            log.error("{}服务器错误!", -1);
            return 1;
        }
        log.info("验证码校验成功{}-{}",email,code);
        //更新状态
        codeFeignService.updateStatus(code);
        SysUser sysUser = new SysUser(password, email);
        //保存数据
        return userMapper.saveUser(sysUser) == 1 ? 0 : 1;
    }

    @Override
    public String findEmailByToken(String token) {
        SysToken sysToken = tokenService.findEmailByToken(token);
        if (sysToken == null) {
            return null;
        }
        return sysToken.getEmail();
    }

    @Override
    public String login(String email, String password, HttpServletRequest request,
                        HttpServletResponse response) {
        SysUser sysUser = userMapper.findUserByEmailAndPassword(email, password);
        if (sysUser == null) {
            log.error("{}用户名或密码错误。",email);
            return "false";
        }
        //生产token
        String token = UUID.randomUUID().toString();
        SysToken sysToken = new SysToken();
        sysToken.setToken(token);
        sysToken.setEmail(email);
        //保存token
        tokenService.saveToken(sysToken);

        //写入 cookie
        Cookie cookie = new Cookie("token", token);
        cookie.setPath("/");
        response.addCookie(cookie);
        return sysUser.getEmail();
    }
}
